<template>
  <el-drawer v-model="isShow" :show-close="false" @close="closeHandler" size="50%">
    <template #title>
      <div class="flex">
        <div>{{ idShow ? "修改商品" : "新增商品" }}</div>
        <div style="margin-top: 10px">
          <span class="close" @click="isShow = false" style="margin-right: 20px">X</span>
          <!-- <span>{{ title }}</span> -->
        </div>
      </div>
    </template>
    <el-form :model="formData" :rules="formRules" ref="formRef" label-width="100px" :inline="false">
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属小区" prop="community_code">
            <el-select v-model="formData.community_code">
              <el-option v-for="item in cateList" :key="item.id" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车位名称" prop="name">
            <el-input placeholder="请输入" v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input placeholder="请输入" type="textarea" v-model="formData.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div style="margin-left: 80%">
        <el-button type="primary" @click="onSubmit"> 确定 </el-button>
        <el-button @click="isShow = false"> 取消 </el-button>
      </div>
    </el-form>
  </el-drawer>
</template>

<script setup>
import { ref } from "vue";
import { postParking, getParkingApi, putParkingApi } from "@/api/parking/index.js";
import { ElMessage } from "element-plus";
import { useCommunityAllList } from "@/hooks/hooks.js";
const cateList = useCommunityAllList();
const emit = defineEmits(["updateList"]);
// 表单数据

const defaultState = {
  community_code: "",
  name: "",
  remark: "",
  house: "",
  lift: null,
};
const formData = reactive({ ...defaultState });
// 验证规则
const formRules = {
  community_code: [{ required: true, message: "请选择", trigger: "blur" }],
  remark: [{ required: true, message: "请选择", trigger: "blur" }],
  name: [{ required: true, message: "请选择", trigger: "blur" }],
};
// 表单ref引用
const formRef = ref();

// 表单提交
const onSubmit = async () => {
  try {
    // 1. 校验数据
    await formRef.value.validate();
    // 2. 整理数据
    // 3. 提交数据
    const data = toRaw(formData);
    formData.id ? await putParkingApi(data) : await postParking(data);
    // 4. 提示成功
    ElMessage.success("操作成功");
    // 5. 关闭
    isShow.value = false;
    // 6. 列表更新
    emit("updateList");
  } catch (error) {
    console.log(error);
  }
};

// 关闭事件
const closeHandler = () => {
  // 移除校验
  formRef.value.resetFields();
  // 重置表单数据
  Object.assign(formData, { ...defaultState });
  idShow.value = "";
};
//弹框标题
const idShow = ref(0);

// 回显数据
const replyData = async (id) => {
  let { data } = await getParkingApi(id);
  Object.assign(formData, data);
  idShow.value = id;
  isShow.value = true;
  console.log(data);
};

// 弹框控制变量
const isShow = ref(false);
defineExpose({
  isShow,
  replyData,
});
</script>
<style scoped>
.close {
  cursor: pointer;
}

.flex {
  display: flex;
  justify-content: space-between;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

:deep(.quill) {
  margin-left: -100px;
}

:deep(.quill .el-form-item__content) {
  display: block;
}
</style>
